﻿using System.Data;
using Dapper;
using DataServer.ApplicationLib.Entities;

namespace DataServer.ApplicationLib.Repositories
{
    public class ConfigDataCheckRepository : IConfigDataCheckRepository
    {
        private readonly IDbConnection _dbConnection;

        public ConfigDataCheckRepository(IDbConnection dbConnection)
        {
            _dbConnection = dbConnection;
        }

        public async Task<bool> UpdateConfigDataCheck(string valueColumnMap, bool isValid, string msg, Guid? configId, Guid? requestId)
        {
            var querySql = $"select Id ,{valueColumnMap} Value from DS_CONFIGDATACHECK where ConfigId = @configId and RequestId = @requestId";

            var list = await _dbConnection.QueryAsync<dynamic>(querySql,
                new { configId = configId, requestId = requestId });

            var flagCount = 0;

            foreach (var item in list)
            {
                if (item.Value == null)
                {
                    var sql = @"update DS_CONFIGDATACHECK set IsValid = @isValid,Message = @msg where Id = @id ";
                    flagCount += await _dbConnection.ExecuteAsync(sql, new { isValid = isValid ? "是" : "否", msg = msg, id = item.Id });
                }
            }

            return flagCount > 0;

        }

        public Task BatchInsertAsync(IEnumerable<ConfigDataCheckEntity> checklist)
        {
            throw new NotImplementedException();
        }
    }
}